from rest_framework import generics
from rest_framework import serializers
from apps.goods.models import GoodsVisitCount, SPUSpecification, SPU, SpecificationOption, SKU ,GoodsCategory
from apps.users.models import User


# class SpuRelatedField(serializers.ModelSerializer):
#     class Meta:
#         model = SPU
#         fields = ["id","name"]
# class SPUSpecificationSerializer(serializers.ModelSerializer):
#     spu = SpuRelatedField(read_only=True)
#     spu = spu["name"]
#     spu_id = serializers.IntegerField()
#     class Meta:
#         model = SPUSpecification
#         fields = ["id","name","spu","spu_id"]

# 规格表序列化器
class SPUSpecificationSerializer(serializers.ModelSerializer):
    # 关联嵌套返回spu表的商品名
    spu = serializers.StringRelatedField(read_only=True)
    # 返回关联spu的id值
    spu_id = serializers.IntegerField()

    class Meta:
        model = SPUSpecification  # 商品规格表关联了spu表的外键spu
        # fields = '__all__'
        fields = ["id", "name", "spu", "spu_id"]

class SPUSerializer(serializers.ModelSerializer):
    class Meta:
        model = SPU
        fields = "__all__"
